/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;
/**
 *
 * @author Kvest
 */
public class RegionList extends Form implements ActionListener
{
    public static final int CHOSE_ID = 1;
    public static final int BACK_ID = 2;

    private Command chose;
    private Command back;
    private FormAction listener;

    private ThumbList regionsList;

    public RegionList(FormAction listener)
    {
        super("Районы");
        this.listener = listener;

        init();
    }

    private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);

        addCommandListener(this);

        back= new Command("Назад", BACK_ID);
        addCommand(back);

        chose = new Command("Выбрать", CHOSE_ID);
        addCommand(chose);

        //Создаем и настраиваем список
        regionsList = new ThumbList();
        regionsList.setSelectCommandText("");
        regionsList.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        
        if (ViewManager.getViewManager().getFunctionMenuType() != FunctionMenu.DONETSK_TYPE)
        {
            TickerRenderer render = new TickerRenderer();
            render.getStyle().setPadding(Component.BOTTOM, 0);
            render.getStyle().setPadding(Component.TOP, 0);
            regionsList.setRenderer(render);
        }
        
        regionsList.getStyle().setPadding(Component.BOTTOM, 0);
        regionsList.getStyle().setPadding(Component.TOP, 0);
        addComponent(BorderLayout.CENTER,regionsList);
    }

    public void actionPerformed(ActionEvent evt)
    {
        if (listener != null)
        {
            listener.commandAction(evt.getCommand().getId());
        }
    }

    public void clear()
    {
        while (regionsList.size() > 0)
        {
            regionsList.getModel().removeItem(0);
        }
    }

    public void add(String regionName)
    {
        regionsList.getModel().addItem(new CellItem(0, regionName));
    }

    public CellItem getSelectedItem()
    {
        return (CellItem)regionsList.getSelectedItem();
    }
}
